using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;

using CNCSoft.EWinform;
using CNCSoft.EWinform.ControlUtils;
//using CNCSoft.EWinform.ERPApps;
using CNCSoft.EWinform.AGrid;
using CNCSoft.EData;

using Common;

namespace CNCSoft.ErpApps.TMS {

	/// <summary>
	/// Summary description for TienMatPhieuThuData.
	/// </summary>
	public class PhieuYeuCauData: TMSDataBase  {

		public enum TL  {
            BieuMau_PhieuYeuCau,
            BieuMau_PhieuYeuCau_ChiTiet
        }

        private string sqlPhieuYeuCau =
            "SELECT [SoPYC],[NguoiYeuCau],[NgayYeuCau],[MucDichYeuCau],[DoiTac],[NguoiDuyet], [NoiDungDuyet], [NgayDuyet] " +
            "FROM [BIEUMAU_PHIEUYEUCAU]" ;

        private string sqlPhieuYeuCauDetail =
            "SELECT [ID],[SoPYC],[NoiDung],[SoLuong],[DonGia],[ThanhTien],[GhiChu] " +
            "FROM [BIEUMAU_PHIEUYEUCAU_CHITIET]" ;

        public PhieuYeuCauData()
        {

            ds.Tables.Add("BieuMau_PhieuYeuCau");
            ds.Tables.Add("BieuMau_PhieuYeuCau_ChiTiet");
          
			initData("('')", true);
		}

        public void initData(string strSoPYC, bool firstLoading)
        {
			if (!firstLoading) {
				getTable(TL.BieuMau_PhieuYeuCau_ChiTiet).Clear();
				getTable(TL.BieuMau_PhieuYeuCau).Clear();
			}
            
            SQLHelper.Fill(getTable(TL.BieuMau_PhieuYeuCau), GlobalVariable.WSInfo.ConnectionString,
                sqlPhieuYeuCau + " WHERE [SoPYC] In " + strSoPYC);

			SQLHelper.Fill(getTable(TL.BieuMau_PhieuYeuCau_ChiTiet), GlobalVariable.WSInfo.ConnectionString,
                sqlPhieuYeuCauDetail + " WHERE [SoPYC] In " + strSoPYC);

			if (firstLoading) {
				// Set primary key(s)
                getTable(TL.BieuMau_PhieuYeuCau).PrimaryKey = new DataColumn[] { getTable(TL.BieuMau_PhieuYeuCau).Columns["SoPYC"] };
				getTable(TL.BieuMau_PhieuYeuCau_ChiTiet).PrimaryKey = new DataColumn[]{getTable(TL.BieuMau_PhieuYeuCau_ChiTiet).Columns["ID"]};

				// Set relations
                ds.Relations.Add("M_D", getTable(TL.BieuMau_PhieuYeuCau).Columns["SoPYC"],
                    getTable(TL.BieuMau_PhieuYeuCau_ChiTiet).Columns["SoPYC"]);

                // Set default values
                getTable(TL.BieuMau_PhieuYeuCau).Columns["SoPYC"].DefaultValue = "";
                getTable(TL.BieuMau_PhieuYeuCau).Columns["NguoiYeuCau"].DefaultValue = GlobalVariable.WSInfo.UserID;
                getTable(TL.BieuMau_PhieuYeuCau).Columns["NgayYeuCau"].DefaultValue = GlobalVariable.WSInfo.WorkingDate;
                getTable(TL.BieuMau_PhieuYeuCau).Columns["MucDichYeuCau"].DefaultValue = "";
                getTable(TL.BieuMau_PhieuYeuCau).Columns["DoiTac"].DefaultValue = "";

                //Detail
                getTable(TL.BieuMau_PhieuYeuCau_ChiTiet).Columns["ID"].AutoIncrement = true;
                getTable(TL.BieuMau_PhieuYeuCau_ChiTiet).Columns["ID"].AutoIncrementStep = 1;
                getTable(TL.BieuMau_PhieuYeuCau_ChiTiet).Columns["NoiDung"].DefaultValue = "";
                getTable(TL.BieuMau_PhieuYeuCau_ChiTiet).Columns["SoLuong"].DefaultValue = 0;
                getTable(TL.BieuMau_PhieuYeuCau_ChiTiet).Columns["DonGia"].DefaultValue = 0;
                getTable(TL.BieuMau_PhieuYeuCau_ChiTiet).Columns["ThanhTien"].DefaultValue = 0;
                getTable(TL.BieuMau_PhieuYeuCau_ChiTiet).Columns["GhiChu"].DefaultValue = "";
			}
           
		}

		public bool createNewRow() {
			try {
                initData("('')", false);

				DataRow dr = getTable(TL.BieuMau_PhieuYeuCau).NewRow();
                vg.DateForGenerating = WSInfo.WorkingDate;   //Generate theo ngay dang nhap
                vg.Prefix = "YC";
                vg.VoucherNoFormatDesign = VoucherNoGenerator.VoucherNoFormat.Prefix_YY_MM_Delimiter_SerialNo;
                dr["SoPYC"] = vg.generateVoucherNo("BieuMau_PhieuYeuCau", "[SoPYC]");
                getTable(TL.BieuMau_PhieuYeuCau).Rows.Add(dr);

				return true;
			}
			catch {
				MessageBox.Show(RS.ERROR_CREATE_NEW_ROW, RS.ERROR_CREATE_NEW_ROW_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return false;
		}

		public bool update() {
			try {
                new SimpleMasterDetailUpdater(sqlPhieuYeuCau, sqlPhieuYeuCauDetail, getTable(TL.BieuMau_PhieuYeuCau), getTable(TL.BieuMau_PhieuYeuCau_ChiTiet), GlobalVariable.WSInfo.ConnectionString);
                ds.AcceptChanges();
				return true;
			} catch (Exception ex) {
				MessageBox.Show(RS.ERROR_UPDATE +"\n" +ex.Message, RS.ERROR_UPDATE_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return false;
		}

		public DataTable getTable(TL index) {
			return ds.Tables[(int)index];
		}

	}
}
